<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2016/11/14 0014
 * Time: 19:11
 * @author:xjw129xjt(駿濤) 531201650@qq.com
 */


function thumb($id, $width = 300, $height = 300)
{
    return model('core/Picture')->thumb($id, $width, $height);
}


function render_picture($ids)
{
    $ids = is_array($ids) ? $ids : explode(',', $ids);
    $html = '';
    $pic = db('picture')->where(['id' => ['in', $ids]])->order("FIND_IN_SET(id,'" . implode(',', $ids) . "')")->select();
    $root = get_root();
    $picture_save_path = '/uploads/picture/';

    foreach ($pic as $picture) {
        $path = $root . $picture_save_path . $picture['path'];
        $html .= '<img src="' . $path . '" style="max-height:200px;display:block" />';
    }

    echo $html;
}


/**
 * 上传指定的本地文件内容
 *
 * @param OssClient $ossClient OSSClient实例
 * @param string $bucket 存储空间名称
 * @param string $object 上传的文件名称
 * @param string $Path 本地文件路径
 * @return null
 */
function upload_file($object, $Path)
{
    $config = config('aliyun_oss');
    //try 要执行的代码,如果代码执行过程中某一条语句发生异常,则程序直接跳转到CATCH块中,由$e收集错误信息和显示
    try {
        //没忘吧，new_oss()是我们上一步所写的自定义函数
        $ossClient = new_oss();
        //uploadFile的上传方法
        $ossClient->uploadFile($config['bucket'], $object, $Path);
    } catch (OssException $e) {
        //如果出错这里返回报错信息
        return $e->getMessage();
    }
    //否则，完成上传操作
    return true;
}


/**
 * 实例化阿里云OSS
 * @return object 实例化得到的对象
 * @return 此步作为共用对象，可提供给多个模块统一调用
 */
function new_oss()
{
    //获取配置项，并赋值给对象$config
    $config = config('aliyun_oss');
    //实例化OSS
    $oss = new \OSS\OssClient($config['key_id'], $config['key_secret'], $config['endpoint']);
    return $oss;
}


//$tail = '!998'
function get_pic_path($path, $tail = '')
{
    return get_image_path($path, $tail);
}

function get_image_path($path, $tail = '')
{
    $url = '';
    if ($path) {
        if (is_bool(strpos($path, 'http://')) && is_bool(strpos($path, 'https://'))) {

            $host = config('img_host');

            $url = model('core/Picture')->renderPicturePath($path);
           // $url = ($host ?: (get_root() . '/')) . ltrim($path, '/') . $tail;
        } else {
            $url = $path;
        }
    }
    return $url;
}



